<template>
  <basic-container>
    <avue-crud ref="crud"
               v-model="form"
               :before-open="beforeOpen"
               :data="data"
               :option="option"
               :page="page"
               :permission="permissionList"
               :table-loading="loading"
               @row-update="rowUpdate"
               @row-save="rowSave"
               @row-del="rowDel"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @on-load="onLoad">
      <template slot="isSign" slot-scope="scope">
        <el-switch v-model="scope.row.isSign" :active-value="1" :inactive-value="0"
                   @change="rowSwitchState(scope.row)"></el-switch>
      </template>
      <template slot="menuLeft">
        <el-button icon="el-icon-delete"
                   plain
                   size="small"
                   type="danger"
                   @click="handleDelete">删 除
        </el-button>
      </template>
      <template slot="menuLeft">
        <el-button icon="el-icon-back" size="small" type="warning" @click.stop="goBack()">返回学生列表</el-button>
      </template>
    </avue-crud>
  </basic-container>
</template>

<script>
import {add, getList, getStudentParentSelect, remove, update} from "@/api/service/xsJz";
import {mapGetters} from "vuex";

export default {
  props: {
    xsId: {
      type: String,
      default: '0'
    },
    tenantId: {
      type: String,
      default: '0'
    },
    showMainView: {
      type: Function,
      default: null
    }
  },
  data() {
    return {
      form: {},
      query: {},
      loading: true,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0
      },
      selectionList: [],
      option: {
        height: 'auto',
        calcHeight: 210,
        searchShow: true,
        searchMenuSpan: 6,
        tip: false,
        border: true,
        index: true,
        viewBtn: true,
        selection: true,
        column: [
          {
            label: "主键",
            prop: "id",
            rules: [{
              required: true,
              message: "请输入主键",
              trigger: "blur"
            }],
            hide: true,
            display: false

          },
          {
            label: "家长",
            prop: "jzId",
            rules: [{
              required: true,
              message: "请输入",
              trigger: "blur"
            }],
            type: "select",
            dicData: [],
            // dicUrl: "/api/ManageStudent/GetStudentParentSelect",
            props: {
              label: "title",
              value: "id"
            },
          },
          {
            label: "学生",
            prop: "xsId",
            rules: [{
              required: true,
              message: "请输入",
              trigger: "blur"
            }],
            hide: true,
            display: false
          },
          {
            label: "家长类型",
            prop: "jzType",
            rules: [{
              required: true,
              message: "请输入家长类型",
              trigger: "blur"
            }],
            type: "select",
            dicData: [
              {
                label: "父亲",
                value: '1'
              },
              {
                label: "母亲",
                value: '2'
              },
              {
                label: "外祖母",
                value: '3'
              },
              {
                label: "外祖父",
                value: '4'
              },
              {
                label: "祖母",
                value: '5'
              },
              {
                label: "祖父",
                value: '6'
              },
              {
                label: "其他",
                value: '100'
              },
            ],
            // dataType: 'number',

          },
          {
            label: "是否允许登录",
            prop: "isSign",
            type: "radio",
            addDisplay: false,
            editDisplay: false,
            viewDisplay: true,
            dicData: [
              {
                label: "否",
                value: 0
              },
              {
                label: "是",
                value: 1
              }
            ],
            // value: 1,
            slot: true,
            // rules: [
            //   {
            //     required: true,
            //     message: "请选择车辆状况",
            //     trigger: "blur"
            //   }
            // ]
          },
        ]
      },
      data: []
    };
  },
  computed: {
    ...mapGetters(["permission"]),
    // permissionList() {
    //   return {
    //     addBtn: this.vaildData(this.permission.jzxs_add, false),
    //     viewBtn: this.vaildData(this.permission.jzxs_view, false),
    //     delBtn: this.vaildData(this.permission.jzxs_delete, false),
    //     editBtn: this.vaildData(this.permission.jzxs_edit, false)
    //   };
    // },
    ids() {
      let ids = [];
      this.selectionList.forEach(ele => {
        ids.push(ele.id);
      });
      return ids.join(",");
    }
  },
  mounted() {
    this.getStudentParentData()
  },
  //
  methods: {
    rowSwitchState(row){
      console.log('row',row)
      console.log('rowBusStatus',row.isSign)
    },
    rowSave(row, done, loading) {
      row.xsId = this.xsId;
      row.id = row.id === '' ? '0' : row.id
      add(row).then(() => {
        done();
        this.onLoad(this.page);
        this.$message({
          type: "success",
          message: "操作成功!"
        });
      }, error => {
        window.console.log(error);
        loading();
      });
    },
    rowUpdate(row, index, done, loading) {
      row.xsId = this.xsId;
      update(row).then(() => {
        done();
        this.onLoad(this.page);
        this.$message({
          type: "success",
          message: "操作成功!"
        });
      }, error => {
        window.console.log(error);
        loading();
      });
    },
    rowDel(row) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          return remove({ids: row.id});
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        });
    },
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          return remove({ids: this.ids});
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          this.$refs.crud.toggleSelection();
        });
    },
    beforeOpen(done, type) {
      if (["edit", "view"].includes(type)) {
        // getDetail(this.form.id).then(res => {
        //   this.form = res.data.data;
        // });
      }
      done();
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    onLoad(page, params = {}) {
      this.loading = true;
      params.xsId = this.xsId;
      getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.selectionClear();
      });
    },
    goBack() {
      this.showMainView();
    },
    getStudentParentData() {
      let params = {
        tenantId: this.tenantId
      }
      getStudentParentSelect(params).then(res => {
        const jzId = this.findObject(this.option.column, "jzId");
        jzId.dicData = res.data.data
      });
    }
  }
};
</script>

<style>
</style>
